import socket
import time
import threading

# 定义TCP地址
tcp_ip = '127.0.0.1'
tcp_port = 8888

# 创建TCP客户端
tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接到服务器
tcp_client_socket.connect((tcp_ip, tcp_port))

def send_data():
    count = 0
    while True:
        message = f"Test message {count}"
        tcp_client_socket.sendall(message.encode())  # 发送数据
        print(f"Sent: {message}")
        count += 1
        time.sleep(1)  # 每秒发送一次

def receive_data():
    while True:
        data = tcp_client_socket.recv(1024)  # 接收数据
        print(f"Received: {data.decode()}")  # 解码并打印接收到的数据
        time.sleep(1)  # 每秒处理一次接收

# 创建发送和接收线程
sender_thread = threading.Thread(target=send_data)
receiver_thread = threading.Thread(target=receive_data)

# 启动线程
sender_thread.start()
print("Sender thread started")
receiver_thread.start()
